<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%

/*
 * Copyright 2006 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

%>

<%@ page import="bw.news.*" %>
<%@ page import="java.util.*" %>
<%

    String newsCategoryId = request.getParameter( "id" );

    NewsManager manager = NewsManager.getInstance();

    Vector sources = null;
    boolean getNewOnly = true;
    boolean hasNews = false;

    NewsCategory cat = null;
    if ( newsCategoryId != null && !newsCategoryId.equals( "" ) ) {
        int id = Integer.parseInt( newsCategoryId );
        cat = manager.getNewsCategoryForId( id );
    }

    String newsSourceId = request.getParameter( "sourceId" );
    if ( newsSourceId != null && !newsSourceId.equals( "" ) ) {
	NewsSource source = manager.getNewsSourceForId( newsSourceId );
	if ( source != null ) {
	  sources = new Vector();
  	  sources.addElement( source );
        }
	getNewOnly = false;
    }
    else if ( cat != null ) {
    	sources = cat.getSortedNewsSources();
    }


%>
<html>
<head>
<base target="_blank">
<link rel=STYLESHEET type="text/css" href="style.jsp">
<style>
IMG { max-width:95%; }
</style>
<script type="text/javascript" src="include/yui_2.6/yahoo-dom-event.js"></script>
<script type="text/javascript" src="include/yui_2.6/connection-min.js"></script>
</head>
<body bgcolor="#003366" style="margin:0px;">

<% if ( sources != null ) { %>

<div id='nitems' class="newsItems">
<%
      Iterator nsrcIterator = sources.iterator();
      while ( nsrcIterator.hasNext() ) {
        NewsSource source = (NewsSource) nsrcIterator.next();
	Iterator items = source.getNewsItems();
	while ( items.hasNext() ) {
          NewsItem item = (NewsItem) items.next();
	  if ( !getNewOnly || item.isNew() || item.isStarred() ) { 
	    hasNews = true;
%>
<div class="newsItem">
<div class="newsTitle">
<table cellpadding="0" cellspacing="0" margin="0" width="100%">
<tr>
<td width="100%"><p class="nsource"><a href="<%= source.getSiteURL() %>"><%= source.getName() %></a></p></td>
<% if ( item.allowsStar() ) { %>
<td align="right" valign="center" style="font-size:small; font-weight:bold;"><div onMouseOver="hoverNewsItemStarOn( this );" onMouseOut="hoverNewsItemStarOff( this );" onClick="toggleNewsItemStar( '<%= source.getIdStr() %>', '<%= item.getId() %>', this ); return false;" style='<%= ( item.isStarred() ? "color:#FFFFFF;" : "color:#2D5986;" ) %>' starred="<%= item.isStarred() %>">&#9733;</div></td>
<% } %>
</tr>
</table>
<% if ( item.hasLink() && item.hasTitle() ) { %>
<p class="ntitle"><a href="<%= item.getLink() %>"><%= item.getTitle() %></a></p>
<% } else if ( item.hasLink() ) { %>
<p class="ntitle"><a href="<%= item.getLink() %>"><%= item.getLink() %></a></p>
<% } else if ( item.hasTitle() ) { %>
<p class="ntitle"><%= item.getTitle() %></p>
<% } %>
</div>
<div class="newsItemText"><%= item.getDescription() %></div>
<% if ( item.hasPublishedDate() ) { %>
<div class="newsDate">
<%= item.getPublishedDateStr() %>
</div>
<% } %>
</div>
<% 
          }
        }
      }

if ( !hasNews ) {    
%>
<div class="noNewsItem">
<b>No news.</b>
</div>
<% } %>
</div>

<% 
    //reset the new items count since we are looking at it now
    if ( cat != null ) {
      cat.resetNewNewsItems();
    }
%>

<% } %>

</body>

<script>

function toggleNewsItemStar ( sourceId, nid, d )
{
  if ( d.getAttribute( "starred" ) == 'false' ) 
  {
    d.style.color = '#FFFFFF';
    d.setAttribute( "starred", "true" );
    starItem( sourceId, nid, true );
  } else {
    d.style.color = '#6699CC';
    d.setAttribute( "starred", "false" );
    starItem( sourceId, nid, false );
  }
}

function starItem ( sourceId, nid, star )
{
  var surl = 'updateNews?action=toggleStar&newsSourceId=' + sourceId + 
    '&newsItemId=' + nid + 
    '&star=' + star;

  var callback = 
  {
    success : handleStarItem,
    failure : handleFailure
  };

  YAHOO.util.Connect.asyncRequest( 'GET', surl, callback );
}

function handleStarItem ( obj )
{
  var message = obj.responseText.replace(/^\s+|\s+$/g, '');
  if ( message != 'OK' )
  {
    alert( "Problem starring item!" );
  }
}

function handleFailure ( obj )
{
  alert( "Problem: " + obj.statusText + " (" + obj.status + ")" ); 
}

function hoverNewsItemStarOn ( d )
{
  d.style.cursor = 'pointer';
  
  if ( d.getAttribute( "starred" ) == 'false' ) 
  {
    d.style.color = '#6699CC';
  }
}

function hoverNewsItemStarOff ( d )
{
  d.style.cursor = 'default';

  if ( d.getAttribute( "starred" ) == 'false' )
  {
    d.style.color = '#2D5986';
  }
}

window.top.document.getElementById( "newsFeedWindow" ).focus();
window.focus();

</script>

</html>
